import { parseTime } from '@/utils'
import { deptTypeMap } from '@/config/dict'
import { validAccount } from '@/utils/validate'
import { CustomStatus } from '@/components'

export const tableColumn = (name, handleEdit, handleDelete) => {
  return [
    {
      key: 'name',
      label: name,
      minWidth: 120
    },
    {
      key: 'createName',
      label: '创建人',
      minWidth: 120
    },
    {
      key: 'status',
      label: '状态',
      minWidth: 100,
      render: (h, scope) => <CustomStatus list={deptTypeMap} value={scope.row.status} />
    },
    {
      key: 'updateName',
      label: '最后修改人',
      minWidth: 140
    },
    {
      key: 'updateTime',
      label: '最后修改时间',
      minWidth: 200,
      render: (h, scope) => parseTime(scope.row.updateTime)
    },
    {
      type: 'button',
      label: '操作',
      list: [
        {
          name: '修改',
          authKey: name === '部门名称' ? 'e_2_4' : 'e_2_7',
          click: handleEdit
        },
        {
          name: '删除',
          authKey: name === '部门名称' ? 'e_2_5' : 'e_2_8',
          confirm: '确认删除吗',
          click: handleDelete
        }
      ]
    }
  ]
}

export const orgFormConfig = (list = [], isHide) => {
  return [
    {
      type: 'select',
      label: '上级机构',
      key: 'parentOrgId',
      otherAttrs: {
        disabled: true
      },
      slotAttrs: {
        options: list.map(item => ({ key: item.id, value: item.orgName }))
      }
    },
    {
      type: 'input',
      label: '机构名称',
      key: 'orgName',
      required: true
    },
    {
      type: 'input',
      label: '管理员账号',
      isHide,
      key: 'account',
      rules: [
        { required: true, message: '管理员账号不能为空' },
        { validator: validAccount, trigger: 'change' }
      ]
    }
  ]
}

export const deptModalFormItem = (orgList = []) => {
  return [
    {
      type: 'select',
      label: '机构',
      key: 'orgId',
      slotAttrs: {
        options: orgList.map(item => ({ key: item.id, value: item.orgName })),
        disabled: true
      },
      required: true
    },
    {
      type: 'input',
      label: '部门名称',
      key: 'name',
      required: true
    },
    {
      type: 'radio',
      label: '状态',
      key: 'status',
      slotAttrs: {
        options: deptTypeMap
      },
      required: true
    }
  ]
}

export const postModalFormItem = (dropDeptList) => {
  return [
    {
      type: 'select',
      label: '部门',
      key: 'deptId',
      slotAttrs: {
        options: dropDeptList,
        disabled: true
      },
      required: true
    },
    {
      type: 'input',
      label: '岗位名称',
      key: 'name',
      required: true
    }
  ]
}

export const formButtons = (onSubmit, onReset) => {
  return {
    isDialog: true,
    submitName: '提交',
    resetName: '取消',
    submitCallback: values => onSubmit(values),
    resetCallback: () => onReset()
  }
}

